<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="135px" @submit.native.prevent>
      <el-form-item label="社交媒体类型" prop="mediaType">
        <el-select v-model="pram.mediaType" placeholder="社交媒体类型" clearable filterable allow-create default-first-option @change="onTypeChange($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.mediaList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="社交媒体账号" prop="mediaNumber">
        <el-input v-model="pram.mediaNumber" maxlength="100" placeholder="社交媒体账号" />
      </el-form-item>
      <el-form-item label="是否存在违规行为" prop="illegal">
        <el-radio-group v-model="pram.illegal">
          <el-radio :label="0">否</el-radio>
          <el-radio :label="1">是</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="所属人" prop="personId">
        <el-select v-model="pram.personId" placeholder="所属人" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.personList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:electronic:account:update','admin:electronic:account:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as electronicApi from '@/api/electronic.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCategoryData:{
      type: Object
    }
  },
  data() {
    /*const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };*/
    return {
      constants:this.$constants,
      pram: {
        mediaType: null,
        mediaNumber: null,
        illegal: 0,
        personId: null,
        number: null,
        id: 0,
      },
      rules: {
        mediaType: [{ required: true, message: '请选择社交媒体类型', trigger: ['blur', 'change'] }],
        mediaNumber: [{ required: true, message: '请填社交媒体账号', trigger: ['blur', 'change'] }],
        illegal: [{ required: true, message: '请选择是否存在违规行为', trigger: ['blur', 'change'] }],
        personId: [ { required: true, message: '请选择所属人', trigger: ['blur', 'change']} ],
      },
      categoryData:{
        mediaList:[],
        personList:[]
      }
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onTypeChange(e) {
      if (e) {
        this.pram.mediaType = e
      }
    },

    initEditData() {
      this.categoryData = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { mediaType, mediaNumber, illegal, personId, id,number } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      //this.pram = {...this.editData}
      this.pram.mediaType = mediaType
      this.pram.mediaNumber = mediaNumber
      this.pram.illegal = illegal
      this.pram.personId = personId
      this.pram.id = id
      this.pram.number = number
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      electronicApi.accountAdd(this.pram).then(data => {
        this.$message.success('创建社交媒体账号成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      electronicApi.accountAdd(this.pram).then(data => {
        this.$message.success('更新社交媒体账号成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
